package com.changjiang.luban.service.sys;

import com.changjiang.luban.dao.sys.SysPermissionDao;
import com.changjiang.luban.dao.sys.SysRoleDao;
import com.changjiang.luban.dao.sys.SysUserDao;
import com.changjiang.luban.dao.domain.sys.SysPermission;
import com.changjiang.luban.dao.domain.sys.SysRole;
import com.changjiang.luban.dao.domain.sys.SysUser;
import com.changjiang.luban.utils.ContrastUtils;
import com.changjiang.luban.utils.data.Page;
import com.changjiang.luban.utils.data.Tree;
import com.changjiang.luban.utils.encryption.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author changjiang
 * Date: 2018-03-29
 * Time: 11:27
 */
@SuppressWarnings("ALL")
@Service
public class SysUserServiceImpl implements SysUserServiceI {

    @Autowired
    private SysUserDao userDao;

    @Autowired
    private SysRoleDao roleDao;

    @Autowired
    private SysPermissionDao permissionDao;


    @Override
    public List<Tree> tree(Long userCode) {
        List<SysPermission> l = permissionDao.findByUserId(userCode);
        List<Tree> lt = Tree.objectToTree(l);
        return lt;
    }

    @Override
    public List<Tree> treeRole(Long roleId) {
        List<SysPermission> l = permissionDao.findByRoleId(roleId);
        List<Tree> lt = Tree.objectToTree(l);
        return lt;
    }

    @Override
    public List<Tree> tree() {
        List<SysPermission> l = permissionDao.queryAll();
        List<Tree> lt = Tree.objectToTree(l);
        return lt;
    }


    @Override
    public SysUser findByLoginName(String loginName) {
        return userDao.findByUserName(loginName);
    }

    @Override
    public List<SysUser> findAll(Long id) {
        return userDao.findAll(id);
    }

    @Override
    public SysUser findById(Long id) {
        return userDao.findInfo(id);
    }

    @Override
    public SysUser login(String loginName, String passWord) {
        SysUser user = findByLoginName(loginName);
        if(user!=null){
            passWord = MD5Utils.encodeMd5(passWord);
            if(passWord.equals(user.getPassWord())){
                return user;
            }
        }
        return null;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Page<SysUser> pageByName(String name, Page page) {
        String sql = "SELECT * FROM sys_user WHERE 1=1 ";
        if(ContrastUtils.isNull(name)){
            sql += " AND  (name LIKE '%"+name+"%'"
              + " or  user_name like '%" + name + "%')";
        }
        sql += " ORDER BY insert_time ";
        return userDao.page(sql,page,SysUser.class);
    }

    @Override
    public boolean saveSysUser(SysUser sysUser,Long roleId) {
        SysRole role = roleDao.findById(roleId).get();
        Set<SysRole> roles = new HashSet<SysRole>();
        roles.add(role);
        sysUser.setRoles(roles);
        return userDao.saveEntity(sysUser);
    }

    @Override
    public boolean delete(Long id) {
        return userDao.deleteEntity(findById(id));
    }

    @Override
    public Page<SysPermission> page(Page page) {
        String sql = "SELECT * FROM sys_permission ORDER BY id,seq";
        return permissionDao.page(sql,page,SysPermission.class);
    }

    @Override
    public Set<SysPermission> findAll(List<String> tree) {
        Set<SysPermission> permissionList = new HashSet<SysPermission>();
        for (int i = 0; i < tree.size() ; i++) {
            SysPermission permission = permissionDao.findById(Long.valueOf(tree.get(i))).get();
            if(!ContrastUtils.isNull(permission)){
                permissionList.add(permission);
            }
        }
        return permissionList;
    }

    @Override
    public boolean updateSysUser(SysUser sysUser) {
        return userDao.update(sysUser);
    }
}
